// pages/draw/draw.js
import { matchApi } from '../../services/api';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isDrawing: false,
    boxAnimation: null,
    particleAnimation: null,
    showResult: false,
    matchInfo: null,
    currentPhotoIndex: 0,
    isDataReady: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initAnimation();
  },

  initAnimation() {
    const animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    });
    
    this.setData({
      boxAnimation: animation
    });
    
    this.startBoxAnimation();
  },

  startBoxAnimation() {
    // 盒子旋转动画
    const animation = this.data.boxAnimation;
    animation.rotate(10).step()
      .rotate(-10).step()
      .rotate(0).step();
    
    this.setData({
      animationData: animation.export()
    });

    // 循环动画
    setTimeout(() => {
      this.startBoxAnimation();
    }, 3000);
  },

  async handleDraw() {
    if (this.data.isDrawing) return;

    this.setData({ 
      isDrawing: true,
      isDataReady: false,
      showResult: false
    });
    
    try {
      wx.showLoading({ title: '抽取中...' });

      // 播放开盒动画
      const animation = this.data.boxAnimation;
      animation.scale(1.2).step()
        .rotate(720).step()
        .scale(1).step();
      
      this.setData({
        animationData: animation.export()
      });

      const result = await matchApi.getRandom();
      
      wx.hideLoading();
      
      // 判断是否匹配到用户
      if (!result || !result.userInfo) {
        wx.showToast({
          title: '暂无匹配用户',
          icon: 'none',
          duration: 2000
        });
        // 重置动画
        setTimeout(() => {
          this.startBoxAnimation();
        }, 2000);
        return;
      }
      
      // 处理匹配数据
      const matchInfo = {
        matchId: result.matchId,
        nickname: result.userInfo.nickname,
        age: result.userInfo.age,
        occupation: result.userInfo.occupation || '未填写',
        photos: result.userInfo.photos || [],
        height: result.userInfo.height,
        education: result.userInfo.education,
        location: result.userInfo.location,
        hobbies: result.userInfo.hobbies,
        selfIntro: result.userInfo.selfIntro,
        avatarUrl: result.userInfo.avatarUrl
      };

      console.log('处理后的匹配信息：', matchInfo);
      
      this.setData({
        matchInfo,
        isDataReady: true
      }, () => {
        setTimeout(() => {
          this.setData({
            showResult: true
          });
        }, 100);
      });

    } catch (err) {
      console.error('抽取失败', err);
      wx.hideLoading();
      wx.showToast({
        title: err.message || '匹配失败，请重试',
        icon: 'none',
        duration: 2000
      });
      // 重置动画
      setTimeout(() => {
        this.startBoxAnimation();
      }, 2000);
    } finally {
      this.setData({ isDrawing: false });
    }
  },

  handleViewDetail() {
    // 将matchInfo转为字符串并编码
    const matchInfoStr = encodeURIComponent(JSON.stringify(this.data.matchInfo));
    
    wx.navigateTo({
      url: `/pages/match-result/match-result?matchInfo=${matchInfoStr}`
    });
  },

  handlePhotoChange(e) {
    this.setData({
      currentPhotoIndex: e.detail.current
    });
  },

  // 添加关闭弹窗方法
  handleCloseModal() {
    this.setData({
      showResult: false,
      currentPhotoIndex: 0  // 重置照片索引
    });
    
    // 重新开始盒子动画
    setTimeout(() => {
      this.startBoxAnimation();
    }, 500);
  }
})